<?php
/**
 * Created by PhpStorm.
 * User: fenfen
 * Date: 2017/1/3
 * Time: 17:47
 */

namespace frontend\controllers;
use bc\controller\FrontController;
use bc\controller\SampleController;
use bc\controller\ApiController;
use bc\model\OrderbindMode;
use bc\model\UserreportModel;
use bc\model\ReportModel;
use bc\model\order\report\ShareModel;
use bc\model\product\QrcodeModel;
use yii\web\Session;

class ReportController extends SampleController
{
    public $aview;
    public function actionIndex()
    {
        return $this->render("index");
    }

    public function actionReport()
    {
        // 生成js sdk签名
        $this->getRequest()->hostInfo;
        $jsConfig = $this->configJsApi();

        $this->aview ['share_url'] = $this->getRequest()->hostInfo.'/product/detail?id=2';
        $this->aview ['js_config'] = $jsConfig;

        
        $session = new Session();
        $user = $session->get('shop_user');
        
        //获取标题数据
//        $bind_no=$this->post('bind_no');
        $bind_no=$this->get('bind_no');
        //根据bind_no获取产品id
        $OrderbindM= new OrderbindMode();
        $OrderbindM->autoId='bind_no';
        $OrderbindD=$OrderbindM->getRowById($bind_no);
        $product_id=$OrderbindD['product_id'];
        $UserreportData=array();
        if(empty($bind_no )||empty($product_id ))
        {
        }else {
            $UserreportM = new UserreportModel();
            $UserreportData = $UserreportM->getInfoFromBindno($product_id, $bind_no);
            $this->getHtmlFromPid($product_id);
            $view ['product_html']=$this->html;
        }
        $row['report_1_share']=$UserreportData['report_1_share'];
        $row['report_2_share']=$UserreportData['report_2_share'];
        $row['report_3_share']=$UserreportData['report_3_share'];
        $row['report_4_share']=$UserreportData['report_4_share'];
        $row['report_5_share']=$UserreportData['report_5_share'];
        $row['report_1_text']=$UserreportData['report_1_text'];
        $row['report_2_text']=$UserreportData['report_2_text'];
        $row['report_3_text']=$UserreportData['report_3_text'];
        $row['report_4_text']=$UserreportData['report_4_text'];
        $row['report_5_text']=$UserreportData['report_5_text'];
        $row['bind_no']=$bind_no;
        $row['product_id']=$product_id;
        
        $shareModel = new ShareModel();
        $share = $shareModel->getRow(['bind_no' => $bind_no]);
        if($share)
        {
        	$row['share'] = $share;
        }
        
        $qrcodeModel = new QrcodeModel();
        $qrcode = $qrcodeModel->getRow(['user_id' => $user['user_id'], 'product_id' => $product_id, 'is_delete' => 0]);
        if($qrcode)
        {
        	$this->aview ['share_url'] .= '&c=' . $qrcode['code'];
        	$view['qrcode'] = $qrcode;
        }
        
        $row['jsapi']=$this->aview;
        
        return $this->render ( 'report',$row );
    }

    public function actionAjaxareportinfo()
    {
        $reportM = new ReportModel();
        //获取标题数据
        $bind_no=$this->get('bind_no');
        //根据bind_no获取产品id
        $OrderbindM= new OrderbindMode();
        $OrderbindM->columns='bc_order_bind.user_id as ouser_id,bc_order_bind.product_id as oproduct_id';
        $OrderbindD=$OrderbindM->getRow(['bind_no'=>$bind_no]);
        $user_id=$OrderbindD[0]['ouser_id'];
        $product_id = $OrderbindD[0]['oproduct_id'];
        $reportinfo=$reportM -> getReportInfoTemp($user_id,$bind_no,$product_id);
        $data=array(
                "msg"=>"获取成功",
	            "result"=>1,
	            "data"=>$reportinfo
        );
        $json_data=json_encode($data,JSON_UNESCAPED_UNICODE);
        $jsoncallback = htmlspecialchars($_REQUEST ['jsoncallback']);//把预定义的字符转换为 HTML 实体。
        echo $jsoncallback . "(" . $json_data . ")";
    }

    //报告模板
    public function actionBreport()
    {
        return $this->render ( 'breport' );
    }

}